package com.example.lib.socket;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.logging.Handler;

public class MyClientSocket extends Thread{

    public String host;
    public int port;
    public Mycallback call;
    public String data;
    //1连接到服务器2连接到pc 0没有连接
    public int status;
    Socket socket;
    InputStream inputs;
    OutputStream out;
    MysendClient msc;
    public  String DCIM;
    public MyClientSocket(String host,int port){
        this.host=host;
        this.port=port;

    }

    public void MyStop(){
        try {
            if (msc != null) {
                msc = null;
            }
            if (socket != null) {
                socket.close();
            }
        }catch (Exception e) {
                e.printStackTrace();
            }
    }
    public void run(){
        try {
            if (DCIM==null){
                System.out.println("没有设置文件路径");
                return;
            }else{

                if (!new File(DCIM).exists()) {
                    System.out.println("文件路径不存在");
                   return;
                }

            }
        System.out.println("host:"+host);
        System.out.println("port:"+port);
        socket = new Socket();
        //设置发送探测包，不然可能远端关闭连接但是我不知道
        socket.setKeepAlive(true);

        InetSocketAddress  isa = new InetSocketAddress(host, port);
        socket.connect(isa,7000);
        inputs=socket.getInputStream();
        // 获取输入流和输出流 输入流和输出流是通过socket对象来进行数据传输的。
        // BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       out=socket.getOutputStream();

            status=1;
            OcrData od=new OcrData();
            od.msgId=2;
            od.ImgStr="连接到服务器";
            call.MyCall(od);
        //发送线程
        msc=new MysendClient(this);
        msc.start();
        revData(data);
        } catch (Exception e) {
            System.out.println("client is false ");
            e.printStackTrace();
        }
    }
    public  void revData(String args) {
        try {
           // host="192.168.10.101";
           // port=80;

            // 创建Socket对象，指定服务端的IP地址和端口号


            // 从控制台读取用户输入
           // BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String message="";
            //message=MySocketData.getSendData(args);
            //out.println(message);
           while (true) {
            // 发送数据到服务器

                byte[] buff=new byte[19];
                int count= inputs.read(buff,0,19);
                if (count==-1){
                    System.out.println("read 失败");
                    OcrData od=new OcrData();
                    od.msgId=-1;
                    od.ImgStr="read 失败";
                    status=0;
                    call.MyCall(od);
                    break;
               }
               System.out.println("rev data success");
               HeadData hd=MySocketData.getHeadData(buff);
                if (hd.head1!=9||hd.head2!=9||hd.head3!=9){
                    for (int i=0;i<count;i++){

                        System.out.print(buff[i]);
                        System.out.print(" ");
                    }
                    System.out.println("head 错误");
                    OcrData od=new OcrData();
                    od.msgId=-2;
                    od.ImgStr="head 错误";
                    call.MyCall(od);
                    continue;
                }
                if (hd.status==1){
                    OcrData od=new OcrData();
                    od.msgId=1;
                    od.ImgStr="连接到pc";
                    status=2;
                    call.MyCall(od);
                    continue;

                }
               if (hd.status==2){

                   OcrData od=new OcrData();
                   od.msgId=4;
                   od.ImgStr="未连接到pc";
                   status=1;
                   call.MyCall(od);
                   continue;

               }else if  (hd.status==4){
                   System.out.println("pc端断开");
                   OcrData od=new OcrData();
                   od.msgId=5;
                   od.ImgStr="pc端断开";
                   status=1;
                   call.MyCall(od);
                   continue;

               }

                if (hd.dataLong>0){
                    if (hd.dataType==1){
                    byte[] buffMsg=new byte[5000];
                    count= inputs.read(buffMsg,0,19);
                    if (count==-1){
                        System.out.println("read 失败2");
                        OcrData od=new OcrData();
                        od.msgId=-1;
                        od.ImgStr="read 失败2";
                        call.MyCall(od);
                        break;
                    }
                    System.out.println("hd.dataLong is " +  hd.dataLong);
                    message = new String(buffMsg,0,hd.dataLong);

                    System.out.println("service response is " +  message);
                    OcrData od=new OcrData();
                    od.msgId=3;
                    od.ImgStr=message;
                    call.MyCall(od);
                    }else{

                        revImgFile(hd);
                    }

                }




                // 将用户输入的信息发送给服务端
               // out.println(message);
            }

            // 关闭连接
            socket.close();
        } catch (Exception e) {
            OcrData od=new OcrData();
            od.msgId=-1;
            od.ImgStr="read 失败";
            call.MyCall(od);
            System.out.println("client is false ");
            e.printStackTrace();
        }
    }
     public void revImgFile(HeadData hd) throws IOException{

         int  count=0;
         byte[] bufName=new byte[hd.fileNameLong];
         count= inputs.read(bufName,0,hd.fileNameLong);
         if (count==-1){
             System.out.println("read 失败2");
             OcrData od=new OcrData();
             od.msgId=-1;
             od.ImgStr="read 失败2";
             call.MyCall(od);
         }

         String fileName=new String(bufName);
         System.out.println("fileName is "+fileName);

         count=0;
         File saveFile = new File(DCIM, fileName);
         FileOutputStream saveImgOut = new FileOutputStream(saveFile);
         byte[] buffMsg;
         while(count<hd.dataLong-hd.fileNameLong){
             buffMsg=new byte[5000];
             int rev= inputs.read(buffMsg,0,5000);
             if (rev==-1){
                 System.out.println("read 失败2");
                 OcrData od=new OcrData();
                 od.msgId=-1;
                 od.ImgStr="read 失败2";
                 call.MyCall(od);
             }
             saveImgOut.write(buffMsg,0,rev);
             count=count+rev;

         }

         saveImgOut.flush();
         saveImgOut.close();
         System.out.println("保存文件成功");
         OcrData od=new OcrData();
         od.msgId=6;
         od.ImgStr=fileName;
         call.MyCall(od);

     }

}
